//
//  WWDoctorDetailViewController.m
//  快医
//
//  Created by 张湾湾 on 16/7/7.
//
//

#import "WWDoctorDetailViewController.h"
#import "WWDoctorHeaderView.h"
#import "WWMiddleView.h"
#import "WWDoctorDetailController.h"
#import "WWTreatmentConditionController.h"
#import "WWPayAttentionModel.h"



@interface WWDoctorDetailViewController ()<UIScrollViewDelegate>

/**头部*/
@property (nonatomic,weak)WWDoctorHeaderView *headView;
/**中间*/
@property (nonatomic,weak)WWMiddleView *middleView;

/**底部的view*/
@property (nonatomic,weak)UIView *bottomView;
/**底部防止button*/
@property (nonatomic,weak)UIButton *bottomButton;

/**scrollView*/
@property (nonatomic,weak)UIScrollView *scrollView;

@property (nonatomic,strong)UITableViewController *doctorDetailController;

@property (nonatomic,strong)UITableViewController *conditionController;

/**标记选择*/
@property (nonatomic,strong)UIButton *selectedButton;

@end

@implementation WWDoctorDetailViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = XHRColorWithRGB(224, 225, 226);
    self.title = @"医生基本信息";
    
    //MARK: - 右边的按钮
    
    UIButton * collectionButton =  [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 25, 25)];
    
    [collectionButton addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [collectionButton setBackgroundImage:[UIImage imageNamed:@"kongxin"] forState:UIControlStateNormal];
    
    [collectionButton setImage:[UIImage imageNamed:@"shixin"] forState:UIControlStateSelected];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:collectionButton];
    
    //MARK: - 两个tableView
    WWDoctorDetailController * doctorDetailController = [[WWDoctorDetailController alloc]init];
    self.doctorDetailController = doctorDetailController;
    
    WWTreatmentConditionController * conditionController = [[WWTreatmentConditionController alloc]init];
    self.conditionController = conditionController;
    
    [self addChildViewController:conditionController];
    [self addChildViewController:doctorDetailController];
    
    //MARK: - setupUI
    [self setupUI];
    
    //MARK: - 约束
    [self setupConstrains];
}



#pragma mark
#pragma mark - 点击星星
- (void)didClickButton:(UIButton*)sender{
    
    self.selectedButton = sender;
    sender.selected = !sender.selected;
}


#pragma mark
#pragma mark - setupUI
- (void)setupUI{
    
    //MARK: - headView
    WWDoctorHeaderView * headView = [[WWDoctorHeaderView alloc]init];
    
    self.headView = headView;
    self.headView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:headView];
    
    self.headView.headerModel = self.wwViewModel;
    
    
    //MARK: - 中间的view
    WWMiddleView * middleView = [[WWMiddleView alloc]init];
    
    //    middleView.leftLabel
    
    middleView.changeViewBlock = ^(BOOL right,UILabel * lable){
        
        //MARK: - block传值,切换tableView实现联动
        if (right) {
            
            [self.scrollView setContentOffset:CGPointMake(XHRScreenWidth, 0) animated:YES];
            
        }else{
            
            [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        }
    };
    
    
    self.middleView = middleView;
    self.middleView.backgroundColor = [UIColor whiteColor];
    //    self.middleView.userInteractionEnabled = NO;
    [self.view addSubview:middleView];
    
    //MARK: - 底部的view
    UIView * bottomView = [[UIView alloc]init];
    self.bottomView = bottomView;
    self.bottomView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:bottomView];
    
    //MARK: - 底部button
    UIButton * bottomButton = [[UIButton alloc]init];
    self.bottomButton = bottomButton;
    [self.bottomButton setBackgroundColor:XHRColorWithRGB(32, 198, 198)];
    self.bottomButton.layer.cornerRadius = 5;
    self.bottomButton.layer.masksToBounds = YES;
    [self.bottomButton setTitle:@"咨询医生" forState:UIControlStateNormal];
    [self.bottomView addSubview:bottomButton];
    
    UIScrollView * scrollView = [[UIScrollView alloc]init];
    scrollView.delegate = self;
    self.scrollView = scrollView;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.contentSize = CGSizeMake(XHRScreenWidth * 2, 0);
    self.scrollView.showsHorizontalScrollIndicator = NO;
    
    [self.view addSubview:scrollView];
    
    //MARK: - 两个tableView
    [self.scrollView addSubview:self.conditionController.view];
    [self.scrollView addSubview:self.doctorDetailController.view];
    
    
    
}
- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    self.conditionController.view.bounds = self.scrollView.bounds;
    self.conditionController.view.xhr_Origin = CGPointMake(0, 0);
    self.doctorDetailController.view.bounds = self.scrollView.bounds;
    self.doctorDetailController.view.xhr_Y = 0;
    self.doctorDetailController.view.xhr_X = self.conditionController.view.xhr_Width;
    
}
#pragma mark
#pragma mark - 约束
- (void)setupConstrains{
    //MARK: - headerView
    [self.headView makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.offset(0);
        make.height.equalTo(150);
        
    }];
    
    //MARK: - 中间
    [self.middleView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headView.bottom).offset(8);
        make.leading.trailing.offset(0);
        make.height.equalTo(50);
        
    }];
    
    //MARK: - 底部的view
    [self.bottomView makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.leading.trailing.equalTo(0);
        make.height.equalTo(60);
    }];
    
    //MARK: - 底部的button
    [self.bottomButton makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-10);
        make.leading.offset(10);
        make.trailing.offset(-10);
        make.height.equalTo(40);
    }];
    
    //MARK: - scroll
    [self.scrollView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.middleView.bottom).offset(3);
        make.leading.trailing.equalTo(0);
        make.bottom.equalTo(self.bottomButton.top).offset(-10);
        
        
    }];
    
}

#pragma mark
#pragma mark - 滚动结束调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    int index = self.scrollView.contentOffset.x / self.view.frame.size.width;
    //        NSLog(@"====%d==========",index);
    
     if (index == 0){
         self.middleView.leftLabel.textColor = XHRColorWithRGB(119, 210, 210);

        self.middleView.rightLabel.textColor = [UIColor lightGrayColor];
    [UIView animateWithDuration:0.2 animations:^{
        
        self.middleView.lineView.transform = CGAffineTransformIdentity;
    }];
    
}else if (index == 1){
    self.middleView.rightLabel.textColor = XHRColorWithRGB(119, 210, 210);
    self.middleView.leftLabel.textColor = [UIColor lightGrayColor];
    
    CGFloat leftX = CGRectGetMaxX(self.middleView.leftLabel.frame);
    CGFloat rightX = CGRectGetMinX(self.middleView.rightLabel.frame);
    
    [UIView animateWithDuration:0.2 animations:^{
        
        self.middleView.lineView.transform = CGAffineTransformMakeTranslation(rightX - leftX + self.middleView.leftLabel.xhr_Width, 0);
    }];
}

}



@end
